package dao.fileDao;

import dao.interfaces.IAlbum;
import model.Album;
import model.Photo;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * User: Станислав
 * Date: 17.08.14
 */
public class AlbumFileDao implements IAlbum {
    @Override
    public void addAlbum(Album album) {
        try {
            FileWriter fileWritter = null;
            List<Album> albumList = null;
            try {
                albumList = getAllAlbum();
                albumList.add(album);
                fileWritter = new FileWriter(Path.getPATH(), false);
                Iterator<Album> iter = albumList.iterator();
                Album tmp = null;
                while (iter.hasNext()) {
                    tmp = iter.next();
                    if (tmp.getPhotoSet().size() == 0)
                        deleteAlbum(tmp);
                    else
                        fileWritter.write(tmp.toString());
                    fileWritter.write("\r\n");
                }

            } finally {
                if (fileWritter != null) {
                    fileWritter.flush();
                    fileWritter.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Override
    public void deleteAlbum(Album album) {
        try {
            FileWriter fileWritter = null;
            List<Album> albumList = null;
            try {
                albumList = getAllAlbum();
                albumList.remove(album);
                fileWritter = new FileWriter(Path.getPATH(), false);
                Iterator<Album> iter = albumList.iterator();
                while (iter.hasNext()) {
                    fileWritter.write(iter.next().toString());
                    fileWritter.write("\r\n");
                }

            } finally {
                if (fileWritter != null) {
                    fileWritter.flush();
                    fileWritter.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Override
    public List<Album> getAllAlbum() {
        List<Album> albumList = new LinkedList<Album>();
        try {
            FileInputStream fin = null;
            Scanner sc = null;
            try {
                fin = new FileInputStream(Path.getPATH());
                sc = new Scanner(fin);
                Album tmp = null;
                while (sc.hasNextLine()) {
                    tmp = new Album(sc.nextLine());
                    albumList.add(tmp);
                }
            } finally {
                if (fin != null) {
                    fin.close();
                }
                if (sc != null) {
                    sc.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return albumList;
    }

    @Override
    public void echo() {
        System.out.println("AlbumFileDao run!");
    }
}
